<?php
/**
 * Created by PhpStorm.
 * User: sagnitude
 * Date: 12/28/2015
 * Time: 0:58
 */
require './vendor/autoload.php';

include('./RedisConn.php');

Predis\Autoloader::register();

function rssiComparator($a, $b) {
    if ($a->rssi == $b->rssi) {
        return 0;
    }
    return $a->rssi < $b->rssi ? 1 : -1;
}

$_client = RedisServer::getInstance()->_conn;

$_triggerType = $_GET['triggerType'];
$_endUserSn = $_GET['endUserSn'];
$_appToken = $_GET['appToken'];
$_jsonBeacons = $_GET['jsonBeacons'];

//$_url = "http://localhost:63344/idsredis-php/index.php?endUserSn=06E8F42D-6D92-4F1B-8519-1390FFE228F5&appToken=88B06E1BCBC69CF46AF11A91C8BF508E&triggerType=1&jsonBeacons=[{\"major\":12003,\"minor\":42409,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-81},{\"major\":10104,\"minor\":50177,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-90},{\"major\":1485,\"minor\":33018,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-77},{\"major\":10801,\"minor\":15188,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-71},{\"major\":1485,\"minor\":34964,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-67},{\"major\":10101,\"minor\":47092,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-81},{\"major\":1485,\"minor\":36076,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-75},{\"major\":1485,\"minor\":30848,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-84},{\"major\":1485,\"minor\":37319,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-74},{\"major\":1485,\"minor\":35013,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-72},{\"major\":10101,\"minor\":47132,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-80},{\"major\":10104,\"minor\":47878,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-83},{\"major\":11005,\"minor\":40635,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-87},{\"major\":11005,\"minor\":40242,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-82},{\"major\":10104,\"minor\":48009,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-74},{\"major\":10104,\"minor\":47764,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-80},{\"major\":10101,\"minor\":47261,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-76},{\"major\":10101,\"minor\":47841,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-78},{\"major\":11005,\"minor\":40402,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-86},{\"major\":10101,\"minor\":46967,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-88},{\"major\":10101,\"minor\":47897,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-83},{\"major\":11005,\"minor\":40340,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-83},{\"major\":10101,\"minor\":47958,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-78},{\"major\":10101,\"minor\":47773,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-77},{\"major\":1485,\"minor\":34496,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-74},{\"major\":1485,\"minor\":29950,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-74},{\"major\":10101,\"minor\":47090,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-75},{\"major\":1485,\"minor\":34726,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-73},{\"major\":10101,\"minor\":47668,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-88},{\"major\":10104,\"minor\":47566,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-79},{\"major\":11005,\"minor\":40650,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-73},{\"major\":1485,\"minor\":34708,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-75},{\"major\":10814,\"minor\":28464,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-78},{\"major\":11005,\"minor\":40556,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-83},{\"major\":10101,\"minor\":46966,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-80},{\"major\":10104,\"minor\":50241,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-78},{\"major\":11005,\"minor\":40245,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-87},{\"major\":10101,\"minor\":47134,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-80},{\"major\":1485,\"minor\":30426,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-82},{\"major\":11005,\"minor\":40473,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-88},{\"major\":1485,\"minor\":34584,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-81},{\"major\":1485,\"minor\":28939,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-86},{\"major\":10101,\"minor\":47812,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-79}]";
//
//$_triggerType = 1;
//$_endUserSn = "06E8F42D-6D92-4F1B-8519-1390FFE228F5";
//$_appToken = "88B06E1BCBC69CF46AF11A91C8BF508E";
//$_jsonBeacons = "[{\"major\":12003,\"minor\":42409,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-81},{\"major\":10104,\"minor\":50177,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-90},{\"major\":1485,\"minor\":33018,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-77},{\"major\":10801,\"minor\":15188,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-71},{\"major\":1485,\"minor\":34964,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-67},{\"major\":10101,\"minor\":47092,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-81},{\"major\":1485,\"minor\":36076,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-75},{\"major\":1485,\"minor\":30848,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-84},{\"major\":1485,\"minor\":37319,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-74},{\"major\":1485,\"minor\":35013,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-72},{\"major\":10101,\"minor\":47132,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-80},{\"major\":10104,\"minor\":47878,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-83},{\"major\":11005,\"minor\":40635,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-87},{\"major\":11005,\"minor\":40242,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-82},{\"major\":10104,\"minor\":48009,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-74},{\"major\":10104,\"minor\":47764,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-80},{\"major\":10101,\"minor\":47261,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-76},{\"major\":10101,\"minor\":47841,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-78},{\"major\":11005,\"minor\":40402,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-86},{\"major\":10101,\"minor\":46967,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-88},{\"major\":10101,\"minor\":47897,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-83},{\"major\":11005,\"minor\":40340,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-83},{\"major\":10101,\"minor\":47958,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-78},{\"major\":10101,\"minor\":47773,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-77},{\"major\":1485,\"minor\":34496,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-74},{\"major\":1485,\"minor\":29950,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-74},{\"major\":10101,\"minor\":47090,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-75},{\"major\":1485,\"minor\":34726,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-73},{\"major\":10101,\"minor\":47668,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-88},{\"major\":10104,\"minor\":47566,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-79},{\"major\":11005,\"minor\":40650,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-73},{\"major\":1485,\"minor\":34708,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-75},{\"major\":10814,\"minor\":28464,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-78},{\"major\":11005,\"minor\":40556,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-83},{\"major\":10101,\"minor\":46966,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-80},{\"major\":10104,\"minor\":50241,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-78},{\"major\":11005,\"minor\":40245,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-87},{\"major\":10101,\"minor\":47134,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-80},{\"major\":1485,\"minor\":30426,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-82},{\"major\":11005,\"minor\":40473,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-88},{\"major\":1485,\"minor\":34584,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-81},{\"major\":1485,\"minor\":28939,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-86},{\"major\":10101,\"minor\":47812,\"proximityUuid\":\"a3fce438-627c-42b7-ab72-dc6e55e137ac\",\"rssi\":-79}]";

$_beacons = json_decode($_jsonBeacons);

usort($_beacons, 'rssiComparator');

$_posIds = [];

while (list($index, $beacon) = each($_beacons)) {
    $K0 = "UMM:POSID:".strtoupper($beacon->proximityUuid).":".$beacon->major.":".$beacon->minor;
    $V0 = $_client->get($K0);
    if ($V0 != null) {
        array_push($_posIds, $V0);
    }
}

$_planIds = [];

while(list($index, $posId) = each($_posIds)) {
    $K1 = "POSID:PLANID:".$posId;
    $V1 = $_client->zrange($K1, 0, -1, 'WITHSCORES');
    if ($V1 != null) {
        array_push($_planIds, array_keys($V1)[0]);
    }
}

$_ads = [];

while(list($index, $planId) = each($_planIds)) {
    $K2 = "ADPLAN:ADC:".$planId;
    $V2 = $_client->get($K2);
    if ($V2 != null) {
        array_push($_ads, $V2);
    }
}

$_ret = "[".join($_ads, ",")."]";

echo $_ret;
